package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Seccont;

public class GenericSeccontDAO extends AutoKeys implements
		org.jetnuke.dao.SeccontDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericSeccontDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SeccontModel#retrieveByPK(int)
	 */
	public Seccont retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("SeccontModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		Seccont s = new Seccont();

		if (rs.next()) {
			fillSeccontFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillSeccontFromResultSet(Seccont s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillSeccontFromResultSet(Seccont, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setARTID(rs.getInt("ARTID"));
		s.setCONTENT(rs.getString("CONTENT"));
		s.setCOUNTER(rs.getInt("COUNTER"));
		s.setSECID(rs.getInt("SECID"));
		s.setSLANGUAGE(rs.getString("SLANGUAGE"));
		s.setTITLE(rs.getString("TITLE"));

		if (logger.isDebugEnabled()) {
			logger.debug("fillSeccontFromResultSet(Seccont, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SeccontModel#update(org.jetnuke.entities.Seccont)
	 */
	public void update(Seccont seccont) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Seccont) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("SeccontModel.update");
		p.setString(1, seccont.getCONTENT());
		p.setString(2, seccont.getSLANGUAGE());
		p.setString(3, seccont.getTITLE());
		p.setInt(4, seccont.getCOUNTER());
		p.setInt(5, seccont.getSECID());
		p.setInt(6, seccont.getARTID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Seccont) - end"); //$NON-NLS-1$
		}
	}

	private void initNewSeccont(Seccont seccont, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewSeccont(Seccont, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, seccont.getCONTENT());
		p.setString(2, seccont.getSLANGUAGE());
		p.setString(3, seccont.getTITLE());
		p.setInt(4, seccont.getCOUNTER());
		p.setInt(5, seccont.getSECID());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewSeccont(Seccont, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SeccontModel#incCounter(int)
	 */
	public void incCounter(int it) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("incCounter(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("SeccontModel.incCounter"));
		p.setInt(1, it);

		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("incCounter(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SeccontModel#addNew(org.jetnuke.entities.Seccont)
	 */
	public int addNew(Seccont article) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Seccont) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("SeccontModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("SeccontModel.lastGeneratedSeccontID"));
		initNewSeccont(article, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Seccont) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SeccontModel#selectAllLimit(int)
	 */
	public List selectAllLimit(int i) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllLimit(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("SeccontModel.selectAllLimit"));
		p.setInt(1, i);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllLimit(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Seccont u = new Seccont();
			fillSeccontFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SeccontModel#selectAllBySECID(int)
	 */
	public List selectAllBySECID(int secid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllBySECID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("SeccontModel.selectAllBySECID"));
		p.setInt(1, secid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllBySECID(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SeccontModel#searchOrderArtidDesc(java.lang.String)
	 */
	public List searchOrderArtidDesc(String query) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderArtidDesc(String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("SeccontModel.searchOrderArtidDesc"));
		p.setString(1, query);
		p.setString(2, query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderArtidDesc(String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SeccontModel#selectOrderCounterDesc(int)
	 */
	public List selectOrderCounterDesc(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderCounterDesc(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("SeccontModel.selectOrderCounterDesc"));
		p.setInt(1, limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderCounterDesc(int) - end"); //$NON-NLS-1$
		}
		return list;
	}
}
